<template lang="html">
  <div class="">

      <div class="tab1" v-loading="loading">
          <div class="title">{{title}}账号</div>
          <div class="form" >
            <div class="form_item" v-if="!queryId">
                <span> <i>*</i> 员工端</span>
                <div style="width:100%;line-height:50px">
                    <el-radio @change="onRadio" v-model="radio" :label="2">测量师端</el-radio>
                    <el-radio @change="onRadio" v-model="radio" :label="1">平台端</el-radio>
                </div>
            </div>
            <div v-if="radio == 2">
                <div class="form_item">
                    <span> <i>*</i> 员工姓名</span>
                    <el-input v-model="form.realName" placeholder="请填写员工姓名"></el-input>
                </div>
                <div class="form_item" v-if="!queryId">
                    <span> <i>*</i> 微信ID</span>
                    <el-input v-model="form.wxId" placeholder="请填写微信ID"></el-input>
                </div>
                <div class="form_item" v-if="!queryId">
                    <span> <i>*</i> 手机号码</span>
                    <el-input v-model="form.phone" placeholder="请填写手机号码"></el-input>
                </div>
                <div class="form_item">
                    <span> <i v-if="!queryId">*</i> 登录密码</span>
                    <el-input v-model="form.password" type="password" show-password placeholder="请填写登录密码"></el-input>
                </div>
            </div>
            <div v-else>
                <div class="form_item">
                    <span> <i>*</i> 角色类型</span>
                    <div style="width:100%">
                      <el-select v-model="form.roleId" placeholder="请选择">
                        <el-option v-for="item in roleOptions" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
                      </el-select>
                    </div>
                </div>
                <div class="form_item">
                    <span> <i>*</i> 员工姓名</span>
                    <el-input v-model="form.realName" placeholder="请填写员工姓名"></el-input>
                </div>
                <div class="form_item">
                    <span> <i>*</i> 手机号码</span>
                    <el-input v-model="form.phone" placeholder="请填写手机号码"></el-input>
                </div>
                <div class="form_item" v-if="!queryId">
                    <span> <i>*</i> 登录账号</span>
                    <el-input v-model="form.userName" placeholder="请填写登录账号"></el-input>
                </div>
                <div class="form_item">
                    <span> <i v-if="!queryId">*</i> 登录密码</span>
                    <el-input v-model="form.password" type="password" show-password placeholder="请填写登录密码"></el-input>
                </div>
            </div>

          </div>
          <div class="submit_form">
              <el-button type="primary" size="medium" @click="submit">提交</el-button>
          </div>
      </div>

  </div>
</template>

<script>
export default {
  data(){
    return {
      loading: false,
      form:{},
      roleId: null,
      checked: false,
      roleOptions: [],
      radio: 2
    }
  },
  computed:{
    title:function(){
      return this.$route.query.id?'编辑':'新增';
    },
    queryId: function () {
      return this.$route.query.id;
    },
    userType: function () {
      return this.$route.query.userType;
    }
  },
  beforeRouteEnter(to, from, next){
    next(vm=>{
      vm.$route.meta.title = vm.title;
    });
  },
  created(){
    this.selectRoleList();
    if(this.userType){
      this.radio = this.userType;
    }
  },
  methods: {
    selectRoleList(){
      this.loading = true;
      this.$http.selectRoleList().then(res=>{
        this.loading = false;
        if(res.data.code == 0){
          this.selectUser(this.queryId);
          this.roleOptions = res.data.data;
        }
      });
    },
    selectUser(id){
      if(id){
        this.$http.selectUser({id}).then(res=>{
          if(res.data.code == 0){
            this.$set(this.form,'realName',res.data.data.realName);
            this.$set(this.form,'roleId',res.data.data.roleId);
            this.$set(this.form,'phone',res.data.data.phone);
          }
        })
      }
    },
    onRadio(e){
      this.form={};
    },
    insertUser(){
      let postData = {};
      if(this.radio == 1){
        postData = {
          userType:this.radio,
          roleId: this.form.roleId,
          realName: this.form.realName,
          phone: this.form.phone,
          userName: this.form.userName,
          password: this.form.password,
        };
      }else{
        postData = {
          userType:this.radio,
          realName: this.form.realName,
          wxId: this.form.wxId,
          phone: this.form.phone,
          password: this.form.password,
        };
      }
      for(var obj in postData){
        if(!postData[obj]){
          this.$message.error('必填项不能为空！');
          return false;
        }
      }

      this.loading = true;
      this.$http.insertUser(postData)
      .then(res=>{
        this.loading = false;
        if(res.data.code == 0){
          this.$message.success(res.data.message);
          this.$router.push('/system/account/account_list');
        }else{
          this.$message.error(res.data.message);
        }
      })
    },
    updateUser(){
      let postData = {};
      if(this.radio == 1){
        postData = {
          id: this.queryId,
          userType:this.radio,
          roleId: this.form.roleId,
          realName: this.form.realName,
          phone: this.form.phone,
          password: this.form.password,
        };
      }else{
        postData = {
          id: this.queryId,
          userType:this.radio,
          realName: this.form.realName,
          password: this.form.password,
        };
      }
      for(var obj in postData){
        if(!postData[obj]){
          if(obj != 'password'){
            this.$message.error('必填项不能为空！');
            return false;
          }
        }
      }
      this.loading = true;
      this.$http.updateUser(postData)
      .then(res=>{
        this.loading = false;
        if(res.data.code == 0){
          this.$message.success(res.data.message);
          this.$router.push('/system/account/account_list');
        }else{
          this.$message.error(res.data.message);
        }
      })
    },
    submit(){
      if(this.$route.query.id){
        this.updateUser();
      }else{
        this.insertUser();
      }
    },

  }
}
</script>

<style lang="css" scoped>
i{font-style: normal;color: #FB3F3F}
.tab1{
  background: #fff;
  margin-bottom: 10px;
  box-sizing: border-box;
  border: 1px solid #E8E8E8;
}
.form{
  width: 80%;
  padding: 20px 0;
  margin-left: 50px;
}
.form .form_item{
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}
.form .form_item span{
  width: 80px;
  font-size: 13px;
  color: #333333;
  text-align: right;
  margin-right: 10px
}
/deep/.form .el-input{
  width: 100%;
}
/deep/.el-checkbox{
  line-height: 30px;
}
/deep/.el-input--small .el-input__inner{
  width: 250px;
}
/deep/.form_item .el-select{
  width: 250px;
}

.title{
  width: 100%;
  color: #4196FF;
  padding: 12px 15px;
  background-color:#DAEBFF;
  box-sizing: border-box;
}

.submit_form{
  margin-top: 20px;
  padding-left: 130px;
  margin-bottom: 30px;
}
</style>
